import {useNavigate,useLocation} from "react-router-dom";
import { Outlet } from 'react-router-dom'
import {checkRouterAuth} from './index'
import {useEffect,useState} from 'react'
const RouterBeforeEach = ()=>{
  const navigate = useNavigate()
  const location = useLocation()
  const [auth,setAuth] = useState(true)
  useEffect(()=>{
    let obj = checkRouterAuth(location.pathname)
    if(!obj.auth){
      setAuth(false)
      navigate('/', {replace: true})
    }
  // eslint-disable-next-line react-hooks/exhaustive-deps
  },[])
  return auth?<Outlet/>:null
} 
export default RouterBeforeEach